package org.jwcloud.project.module.zx.matClient.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.jwcloud.project.module.zx.baseManager.service.repository.MatBaseManagerRepository;
import org.jwcloud.project.module.zx.common.FmtUtil;
import org.jwcloud.project.module.zx.common.Resp;
import org.jwcloud.project.module.zx.common.repository.RptCommonRepository;
import org.jwcloud.project.module.zx.common.service.RptUtService;
import org.jwcloud.project.module.zx.matClient.service.MatClientService;
import org.jwcloud.project.module.zx.matClient.service.MatSystemService;
import org.jwcloud.project.module.zx.matClient.service.repository.MatSystemRepository;
import org.jwcloud.project.module.zx.workTime.service.repository.MatWorkTimeRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.github.pagehelper.PageHelper;
import com.jw.mid.mm.comm.common.controller.BaseController;
import com.jw.mid.mm.comm.common.controller.CrudController;
import com.jw.mid.mm.comm.common.json.Response;
import com.jw.mid.mm.comm.common.pagination.ListBounds;
import com.jw.mid.mm.comm.common.pagination.ListSlice;
import com.jw.mid.mm.comm.common.pagination.PageListSlice;
import com.jw.mid.mm.comm.common.pagination.Pagination;
import com.jw.mid.mm.comm.common.service.CrudService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

//@Api(value = "API - Controlle",
//description = "服务端（主要是3个审批的12个页面）", tags = "system")
//@RestController
//@Transactional
//@RequestMapping("/rest/mat/system")
public class MatSystemController extends BaseController{
	
	@Autowired
	public RptCommonRepository rptCommonRepository;
	
	@Autowired
	public MatSystemService matSystemService;
	
	@Autowired
    public RptUtService rptUtService;
	
	@Autowired
	public MatSystemRepository matSystemRepository;
	
	@RequestMapping("/myTest")
	public ModelAndView myTest(@RequestParam Map param, ModelAndView model) {
		System.out.println("model: ------- " + param);
		model.addAllObjects(param);
		model.setViewName("test/system/myTest.html");
		return model;
	}
	
	@RequestMapping("/view-all")
	public ModelAndView testView(@RequestParam Map param, ModelAndView model) {
		System.out.println("model: ------- " + param);
		model.addAllObjects(param);
		model.setViewName("test/system/view-all.html");
		return model;
	}
	@RequestMapping("/view-sp")
	public ModelAndView view_sp(@RequestParam Map param, ModelAndView model) {
		System.out.println("model: ------- " + param);
		model.addAllObjects(param);
		model.setViewName("test/system/view-sp.html");
		return model;
	}
	@RequestMapping("/view-bank")
	public ModelAndView view_bank(@RequestParam Map param, ModelAndView model) {
		System.out.println("model: ------- " + param);
		model.addAllObjects(param);
		model.setViewName("test/system/view-bank.html");
		return model;
	}
	
	@GetMapping("queryCommonList")
    @ApiOperation(value = "通用查询接口", notes = "")
    @ApiImplicitParams({
//    	@ApiImplicitParam(name = "sq_state", value = "申请状态 11收卷登记 12材料审核 13材料核准 14待归档 99已归档", required = true, dataType = "string", paramType = "query"),
//    	@ApiImplicitParam(name = "yfk_state", value = "预付款状态 0未开始 21收卷登记 22材料审核 23材料核准 24待归档 99已归档", required = true, dataType = "string", paramType = "query"),
//    	@ApiImplicitParam(name = "js_state", value = "结算款状态 0未开始 31收卷登记 32材料审核 33材料核准 34待归档 99已归档", required = true, dataType = "string", paramType = "query"),
    	@ApiImplicitParam(name = "pageType", value = "申请sq 预付款yfk 结算js", required = true, dataType = "string", paramType = "query"),
    	@ApiImplicitParam(name = "current_process_code", value = "关键参数", required = true, dataType = "string", paramType = "query"),
    	@ApiImplicitParam(name = "query_now_state", value = "已收卷1  审核不通过2 已归档99", required = true, dataType = "string", paramType = "query"),
    	
    	@ApiImplicitParam(name = "id", value = "id", required = false, dataType = "string", paramType = "query"),
//    	@ApiImplicitParam(name = "state", value = "", required = true, dataType = "string", paramType = "query"),
    	@ApiImplicitParam(name = "xqmc", value = "小区名称", required = false, dataType = "string", paramType = "query"),
//    	@ApiImplicitParam(name = "xqmc_all", value = "是否查询全部小区 yes/no", required = false, dataType = "string", paramType = "query"),
    	@ApiImplicitParam(name = "yw_code", value = "业务编码", required = false, dataType = "string", paramType = "query"),
    	
    	@ApiImplicitParam(name = "sbr_name", value = "申报人姓名", required = false, dataType = "string", paramType = "query"),
    	
//    	@ApiImplicitParam(name = "sq_start_date_start", value = "申报时间yyyy-mm-dd hh24:mi:ss", required = false, dataType = "string", paramType = "query"),
//    	@ApiImplicitParam(name = "sq_start_date_end", value = "申报时间", required = false, dataType = "string", paramType = "query"),
//    	@ApiImplicitParam(name = "sq_v1_done_date_start", value = "收卷时间", required = false, dataType = "string", paramType = "query"),
//    	@ApiImplicitParam(name = "sq_v1_done_date_end", value = "收卷时间", required = false, dataType = "string", paramType = "query"),
//    	@ApiImplicitParam(name = "sq_v2_done_date_start", value = "审核时间", required = false, dataType = "string", paramType = "query"),
//    	@ApiImplicitParam(name = "sq_v2_done_date_end", value = "审核时间", required = false, dataType = "string", paramType = "query"),
//    	@ApiImplicitParam(name = "sq_v3_done_date_start", value = "核准时间", required = false, dataType = "string", paramType = "query"),
//    	@ApiImplicitParam(name = "sq_v3_done_date_end", value = "核准时间", required = false, dataType = "string", paramType = "query"),
//    	@ApiImplicitParam(name = "sq_v4_done_date_start", value = "归档时间", required = false, dataType = "string", paramType = "query"),
//    	@ApiImplicitParam(name = "sq_v4_done_date_end", value = "归档时间", required = false, dataType = "string", paramType = "query"),
//    	
//    	@ApiImplicitParam(name = "yfk_start_date_start", value = "申报时间yyyy-mm-dd hh24:mi:ss", required = false, dataType = "string", paramType = "query"),
//    	@ApiImplicitParam(name = "yfk_start_date_end", value = "申报时间", required = false, dataType = "string", paramType = "query"),
//    	@ApiImplicitParam(name = "yfk_v1_done_date_start", value = "收卷时间", required = false, dataType = "string", paramType = "query"),
//    	@ApiImplicitParam(name = "yfk_v1_done_date_end", value = "收卷时间", required = false, dataType = "string", paramType = "query"),
//    	@ApiImplicitParam(name = "yfk_v2_done_date_start", value = "审核时间", required = false, dataType = "string", paramType = "query"),
//    	@ApiImplicitParam(name = "yfk_v2_done_date_end", value = "审核时间", required = false, dataType = "string", paramType = "query"),
//    	@ApiImplicitParam(name = "yfk_v3_done_date_start", value = "核准时间", required = false, dataType = "string", paramType = "query"),
//    	@ApiImplicitParam(name = "yfk_v3_done_date_end", value = "核准时间", required = false, dataType = "string", paramType = "query"),
//    	@ApiImplicitParam(name = "yfk_v4_done_date_start", value = "归档时间", required = false, dataType = "string", paramType = "query"),
//    	@ApiImplicitParam(name = "yfk_v4_done_date_end", value = "归档时间", required = false, dataType = "string", paramType = "query"),
//    	
//    	@ApiImplicitParam(name = "js_start_date_start", value = "申报时间yyyy-mm-dd hh24:mi:ss", required = false, dataType = "string", paramType = "query"),
//    	@ApiImplicitParam(name = "js_start_date_end", value = "申报时间", required = false, dataType = "string", paramType = "query"),
//    	@ApiImplicitParam(name = "js_v1_done_date_start", value = "收卷时间", required = false, dataType = "string", paramType = "query"),
//    	@ApiImplicitParam(name = "js_v1_done_date_end", value = "收卷时间", required = false, dataType = "string", paramType = "query"),
//    	@ApiImplicitParam(name = "js_v2_done_date_start", value = "审核时间", required = false, dataType = "string", paramType = "query"),
//    	@ApiImplicitParam(name = "js_v2_done_date_end", value = "审核时间", required = false, dataType = "string", paramType = "query"),
//    	@ApiImplicitParam(name = "js_v3_done_date_start", value = "核准时间", required = false, dataType = "string", paramType = "query"),
//    	@ApiImplicitParam(name = "js_v3_done_date_end", value = "核准时间", required = false, dataType = "string", paramType = "query"),
//    	@ApiImplicitParam(name = "js_v4_done_date_start", value = "归档时间", required = false, dataType = "string", paramType = "query"),
//    	@ApiImplicitParam(name = "js_v4_done_date_end", value = "归档时间", required = false, dataType = "string", paramType = "query"),
    	
    	@ApiImplicitParam(name = "sq_refuse", 
    		value = "归档99 申请驳回 0未驳回 1驳回（对应使用申请审批四个页面的下拉选：新申报(第一个选项)...0/材料不全(第二个选项)...1）"
    		, required = true, dataType = "string", paramType = "query"),
    	
    	@ApiImplicitParam(name = "yfk_refuse", 
			value = "归档99 预付款驳回 0未驳回 1驳回（对应使用申请审批四个页面的下拉选：新申报...0/材料不全...1）"
			, required = true, dataType = "string", paramType = "query"),
    	
    	@ApiImplicitParam(name = "js_refuse", 
			value = "归档99 结算驳回 0未驳回 1驳回（对应使用申请审批四个页面的下拉选：新申报...0/材料不全...1）"
			, required = true, dataType = "string", paramType = "query"),
    })
    public Resp queryCommonList(
    		@RequestParam(required = false) Map<String,String> param
    		){
		
		return this.matSystemService.queryCommonList(param);
    }
	
	@PostMapping("saveSb")
    @ApiOperation(value = "保存申报", notes = "")
    @ApiImplicitParams({
    	@ApiImplicitParam(name = "xqmc", value = "小区名称", required = true, dataType = "string", paramType = "query"),
    	
    })
    public Resp saveSb(
    		@RequestBody(required = false) Map<String, Object> param
    		){
		String flag = FmtUtil.nullRes(param
				, "xqmc", "小区名称"
				, "sbr_phone", "申报人联系电话");
		if(!StringUtils.isEmpty(flag)) 
			return Resp.error(flag);
		
		return null;
    }
	
	@GetMapping("queryItemList_byBaseId")
    @ApiOperation(value = "查询要件--服务端", notes = "")
    @ApiImplicitParams({
    	@ApiImplicitParam(name = "id", value = "id", required = false, dataType = "list", paramType = "query"),
    })
    public Resp queryItemList_byBaseId(
    		@RequestParam(required = false) Map<String, Object> param
    		){
		return this.matSystemService.queryItemList_byBaseId(param);
    }
	
	@PostMapping("go_state")
    @ApiOperation(value = "审批", notes = "")
    @ApiImplicitParams({
    	@ApiImplicitParam(name = "id", value = "id", required = false, dataType = "string", paramType = "query"),
    	@ApiImplicitParam(name = "process_id", value = "process_id", required = false, dataType = "string", paramType = "query"),
    	@ApiImplicitParam(name = "now_state", value = "当前状态0 11 12 13 14 0 21 22 23 24 0 31 32 33 34", 
    		required = false, dataType = "string", paramType = "query"),
    	@ApiImplicitParam(name = "go_state", value = "1通过 2驳回 99归档", required = false, dataType = "string", paramType = "query"),
    	@ApiImplicitParam(name = "sp_content", value = "驳回（审批）说明", required = false, dataType = "string", paramType = "query"),
    	@ApiImplicitParam(name = "pageType", value = "页面标识 sq yfk js", required = false, dataType = "string", paramType = "query"),
    })
    public Resp go_state(
    		@RequestBody(required = false) Map<String, Object> param
    		){
		
		String flag = FmtUtil.nullRes(param
				, "id", "id"
				, "process_id", "流程id"
				, "now_state", "当前状态"
				, "go_state", "审批状态"
				, "pageType", "页面标识");
		if(!StringUtils.isEmpty(flag)) 
			return Resp.error(flag);
		
		return this.matSystemService.go_state(param);
    }
	
	
	@GetMapping("queryOne")
    @ApiOperation(value = "查单个（客户端用）", notes = "")
    @ApiImplicitParams({
    	@ApiImplicitParam(name = "yw_code", value = "业务编码", required = false, dataType = "string", paramType = "query"),
    	@ApiImplicitParam(name = "xqmc", value = "小区名称", required = false, dataType = "string", paramType = "query"),
    	@ApiImplicitParam(name = "wxxm", value = "维修项目", required = false, dataType = "string", paramType = "query"),
    	@ApiImplicitParam(name = "id", value = "id", required = false, dataType = "string", paramType = "query"),
    	
    	@ApiImplicitParam(name = "queryType", value = "by_yw_code按业务编码查 by_name按小区名称、维修项目", required = false, dataType = "string", paramType = "query"),
    	
    	@ApiImplicitParam(name = "sqType", value = "yfk预付款 js结算", required = true, dataType = "string", paramType = "query"),
    	
    })
    public Resp queryOne(
    		@RequestParam(required = false) Map<String,String> param
    		){
		String flag = FmtUtil.nullRes(param
				, "sqType", "sqType");
		if(!StringUtils.isEmpty(flag)) 
			return Resp.error(flag);
		
		return this.matSystemService.queryOne(param);
    }
	
	@PostMapping("doYfk")
    @ApiOperation(value = "预付款", notes = "")
    @ApiImplicitParams({
    	@ApiImplicitParam(name = "id", value = "id", required = false, dataType = "string", paramType = "query"),
    	@ApiImplicitParam(name = "yfk_fk_money", value = "工程预付款金额金额（元）", required = false, dataType = "string", paramType = "query"),
    	@ApiImplicitParam(name = "yfk_gap_money", value = "资金缺口（元）", required = false, dataType = "string", paramType = "query"),
    	@ApiImplicitParam(name = "yfk_sbr_name", value = "申报人姓名", required = false, dataType = "string", paramType = "query"),
    	@ApiImplicitParam(name = "yfk_id_card", value = "身份证号", required = false, dataType = "string", paramType = "query"),
    	@ApiImplicitParam(name = "yfk_sbr_phone", value = "申报人联系电话", required = false, dataType = "string", paramType = "query"),
    })
    public Resp doYfk(
    		@RequestBody(required = false) Map<String, Object> param
    		){
		
		String flag = FmtUtil.nullRes(param
				, "id", "id"
				, "yfk_fk_money", "工程预付款金额金额（元）"
				, "yfk_gap_money", "资金缺口（元）"
				, "yfk_sbr_name", "申报人姓名"
				, "yfk_id_card", "身份证号"
				, "yfk_sbr_phone", "申报人联系电话");
		if(!StringUtils.isEmpty(flag)) 
			return Resp.error(flag);
		
		return this.matSystemService.doYfk(param);
    }
	
	@PostMapping("doJs")
    @ApiOperation(value = "结算申请", notes = "")
    @ApiImplicitParams({
    	@ApiImplicitParam(name = "id", value = "id", required = false, dataType = "string", paramType = "query"),
    	@ApiImplicitParam(name = "js_money", value = "工程结算金额金额（元）", required = false, dataType = "string", paramType = "query"),
    	@ApiImplicitParam(name = "js_ws_money", value = "维修资金划转金额（元）", required = false, dataType = "string", paramType = "query"),
    	@ApiImplicitParam(name = "js_sbr_name", value = "申报人姓名", required = false, dataType = "string", paramType = "query"),
    	@ApiImplicitParam(name = "js_id_card", value = "身份证号", required = false, dataType = "string", paramType = "query"),
    	@ApiImplicitParam(name = "js_sbr_phone", value = "申报人联系电话", required = false, dataType = "string", paramType = "query"),
    })
    public Resp doJs(
    		@RequestBody(required = false) Map<String, Object> param
    		){
		
		String flag = FmtUtil.nullRes(param
				, "id", "id"
				, "js_money", "工程结算金额金额（元）"
				, "js_ws_money", "维修资金划转金额（元）"
				, "js_sbr_name", "申报人姓名"
				, "js_id_card", "身份证号"
				, "js_sbr_phone", "申报人联系电话");
		if(!StringUtils.isEmpty(flag)) 
			return Resp.error(flag);
		
		return this.matSystemService.doJs(param);
    }
	
	
	@GetMapping("queryMoneySpList")
    @ApiOperation(value = "财务审批", notes = "")
    @ApiImplicitParams({
    	@ApiImplicitParam(name = "sbr_name", value = "申报人姓名", required = false, dataType = "string", paramType = "query"),
    	@ApiImplicitParam(name = "xqmc", value = "小区名称", required = false, dataType = "string", paramType = "query"),
    	@ApiImplicitParam(name = "wxxm", value = "维修项目", required = false, dataType = "string", paramType = "query"),
    	@ApiImplicitParam(name = "hz_time_start", value = "核准时间 yyyy/mm/dd hh24:mi:ss", required = false, dataType = "string", paramType = "query"),
    	@ApiImplicitParam(name = "hz_time_end", value = "核准时间 yyyy/mm/dd hh24:mi:ss", required = false, dataType = "string", paramType = "query"),
    	
    })
    public Resp queryMoneySpList(
    		@RequestParam(required = false) Map<String,String> param
    		){
		
		return this.matSystemService.queryMoneySpList(param);
    }
	
	
	@PostMapping("moneySpGo")
    @ApiOperation(value = "结算申请", notes = "")
    @ApiImplicitParams({
    	@ApiImplicitParam(name = "id", value = "id", required = false, dataType = "string", paramType = "query"),
    	@ApiImplicitParam(name = "bohui_content", value = "驳回说明（业务上不允许驳回，保留字段）", required = false, dataType = "string", paramType = "query"),
    })
    public Resp moneySpGo(
    		@RequestBody(required = false) Map<String, Object> param
    		){
		
		String flag = FmtUtil.nullRes(param
				, "id", "id");
		if(!StringUtils.isEmpty(flag)) 
			return Resp.error(flag);
		
		return this.matSystemService.moneySpGo(param);
    }
	
	@GetMapping("queryBankList")
    @ApiOperation(value = "通知银行划款", notes = "")
    @ApiImplicitParams({
    	@ApiImplicitParam(name = "sbr_name", value = "申报人姓名", required = false, dataType = "string", paramType = "query"),
    	@ApiImplicitParam(name = "xqmc", value = "小区名称", required = false, dataType = "string", paramType = "query"),
    	@ApiImplicitParam(name = "wxxm", value = "维修项目", required = false, dataType = "string", paramType = "query"),
    	@ApiImplicitParam(name = "sp_time_start", value = "财务通过时间 yyyy/mm/dd hh24:mi:ss", required = false, dataType = "string", paramType = "query"),
    	@ApiImplicitParam(name = "sp_time_end", value = "财务通过时间 yyyy/mm/dd hh24:mi:ss", required = false, dataType = "string", paramType = "query"),
    	
    	@ApiImplicitParam(name = "yj_time_start", value = "移交时间 yyyy/mm/dd hh24:mi:ss", required = false, dataType = "string", paramType = "query"),
    	@ApiImplicitParam(name = "yj_time_end", value = "移交时间 yyyy/mm/dd hh24:mi:ss", required = false, dataType = "string", paramType = "query"),
    	
    })
    public Resp queryBankList(
    		@RequestParam(required = false) Map<String,String> param
    		){
		
		return Resp.success(this.rptUtService.pageBuild(
				() -> this.matSystemRepository.queryBankList(param), param));
    }
	
	
	@PostMapping("doYj")
    @ApiOperation(value = "移交", notes = "")
    @ApiImplicitParams({
    	@ApiImplicitParam(name = "rel_base_id", value = "rel_base_id", required = false, dataType = "string", paramType = "query"),
    	@ApiImplicitParam(name = "sp_bat", value = "sp_bat", required = false, dataType = "string", paramType = "query"),
    	@ApiImplicitParam(name = "type_flag", value = "type_flag", required = false, dataType = "string", paramType = "query"),
    })
    public Resp doYj(
    		@RequestBody(required = false) Map<String, Object> param
    		){
		
		String flag = FmtUtil.nullRes(param
				, "rel_base_id", "rel_base_id"
				, "sp_bat", "sp_bat"
				, "type_flag", "type_flag");
		if(!StringUtils.isEmpty(flag)) 
			return Resp.error(flag);
		
		return Resp.success(this.matSystemRepository.doYj(param));
    }
	
}